import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-09-03
 * Time: 20:31
 */
public class test_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String[] strs = new String[n];
            String str1 = in.next();
            String str2 = in.next();
            for (int i = 0; i < n; i++) {
                strs[i] = in.next();
            }
            int s1=-1;
            int s2=-1;
            int ret=Integer.MAX_VALUE;
            for (int i=0 ;i<n;i++){

                    if (strs[i].equals(str1)){
                        s1=i;
                    }
                    if (strs[i].equals(str2)){
                        s2=i;
                    }
                    if (s1!=-1&&s2!=-1){
                        ret=Math.min(ret,Math.abs(s1-s2));
                    }

            }
            System.out.println(ret==Integer.MAX_VALUE?-1:ret);
        }
    }
}
